package com.duobab.springbootsecuritydemo.controller;

import com.duobab.springbootsecuritydemo.domain.Person;
import com.duobab.springbootsecuritydemo.security.SsoContextHolder;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/grade")
public class GradeController {
    private static Map<String, Person> personMap = new HashMap();

    static {
        personMap.put("duobabS", Person.ofPerson(18, "duobabS"));
        personMap.put("duobabT", Person.ofPerson(30, "duobabT"));
    }

    @RequestMapping(method = RequestMethod.GET)
    public Person getGrade() throws JsonProcessingException {
        return personMap.get(SsoContextHolder.getUserInfo().getUsername());
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<Person> getAllGrade() {
        return new ArrayList<>(personMap.values());
    }
}
